package org.jcut.controller;

import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

import org.jcut.bean.User;
import org.jcut.data.Data;
import org.jcut.service.UserService;
import org.jcut.tools.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private Data data;
	@Autowired
	private UserService service;
	@RequestMapping("/login")
	public Object login(User user) {
		System.out.println(user.getPassword());
		System.out.println(user.getUsername());
		user.setPassword(MD5Util.encode2hex(user.getPassword()));
		User u=service.login(user);
		if(u!=null) {
			data.setMyData(1, "登陆成功", u);
		}else {
			data.setMyData(0, "用户名或密码错误", 0);
		}
	
		return data;
	}
	@RequestMapping("/selectAll")
	public Object selectAll(User user,@RequestParam(value="pageNum",required = false)String pageNum,@RequestParam(value="pageSize",required = false) String pageSize) {
		if ((pageNum != null && !("".equals(pageNum))) && (pageSize != null && !("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		}
		
		data.setMyData(1, "查询成功", new PageInfo<>(service.selectAll(user)));
		
		
		return data;
	}
	/**
	 * @author 王征
	 * @param username 用户名必须为手机号
	 * @param password 密码
	 * 
	 * */
	@RequestMapping("/add")
	public Object add(@Valid User user,Errors error) {
		List<ObjectError> e=error.getAllErrors();
		if(e.size()>0) {
			for(ObjectError ee:e) {
				data.setMyData(0,ee.getDefaultMessage(), 0);
			}
		}else {
			user.setPassword(MD5Util.encode2hex(user.getPassword()));
			user.setId(UUID.randomUUID().toString());
			int i=service.insert(user);
			data.setMyData(i,i>0?"注册成功":"服务器异常", i);	
		}
		
		return data;
	}
	/**
	 * @author 王征
	 * @param username 用户名
	 * 
	 * */
	@RequestMapping("/checkExsit")
	public Object checkExsit(String username) {
		long i=service.checkExsit(username);
		data.setMyData(i>0?0:1,i>0?"改账户已被注册":"可以注册", i);
		return data;
	}
	@RequestMapping("/updateDis")
	public Object updateDis(User user) {
		int i=service.updateDis(user);
		data.setMyData(i,i>0?"修改成功":"修改失败", i);
		return data;
	}
	@RequestMapping("/selectByTel")
	public Object selectBytel(String username) {
		data.setMyData(1,"查询成功",service.selectBytel(username));
		return data;
	}
	@RequestMapping("/updatePwd")
	public Object updatePwd(@Valid User user,Errors error) {
		List<ObjectError> e=error.getAllErrors();
		if(e.size()>0) {
			for(ObjectError ee:e) {
				data.setMyData(0,ee.getDefaultMessage(), "0");
			}
		}else {
			user.setPassword(MD5Util.encode2hex(user.getPassword()));			
			int i=service.updateByPrimaryKey(user);
			data.setMyData(i,i>0?"修改成功":"用户名错误", i);
		}
		
		
		return data;
	}
}
